package com.pn.warehouse.controller;

import com.pn.warehouse.dto.AssignAuthDto;
import com.pn.warehouse.entity.CurrentUser;
import com.pn.warehouse.entity.Result;
import com.pn.warehouse.entity.Role;
import com.pn.warehouse.page.Page;
import com.pn.warehouse.service.RoleService;
import com.pn.warehouse.utils.TokenUtils;
import com.pn.warehouse.utils.WarehouseConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RequestMapping("/role")
@RestController
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private TokenUtils tokenUtils;

    /**
     * 查询所有的角色
     */
    @RequestMapping("/role-list")
    public Result roleList(){
        //1、执行业务并返回
        List<Role> roles = roleService.queryAllRole();
        return Result.ok(roles);
    }

    /**
     * 分页查询角色信息
     */
    @RequestMapping("/role-page-list")
    public Result roleListPage(Page page, Role role){
        //1、执行业务并返回
        page = roleService.queryRolePage(page, role);
        return Result.ok(page);
    }

    /**
     * 新增角色
     */
    @RequestMapping("/role-add")
    public Result addRole(@RequestBody Role role, @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token){
        //1、在token中拿到当前登陆用户的id
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        int userId = currentUser.getUserId();

        //2、设置创建人的id
        role.setCreateBy(userId);

        //3、执行业务并返回
        Result result = roleService.insertRole(role);
        return result;
    }

    /**
     * 根据id更新角色的状态
     */
    @RequestMapping("/role-state-update")
    public Result updateRoleState(@RequestBody Role role){
        //1、执行业务并返回
        Result result = roleService.updateRoleStateById(role);
        return result;
    }

    /**
     * 根据id删除角色
     */
    @RequestMapping("/role-delete/{roleId}")
    public Result deleteRole(@PathVariable Integer roleId){
        //1、执行业务并返回
        Result result = roleService.removeRoleById(roleId);
        return result;
    }

    /**
     * 查询角色分配的所有权限菜单并回显
     */
    @RequestMapping("/role-auth")
    public Result queryRoleAuth(Integer roleId){
        //1、执行业务并返回
        List<Integer> authIdsById = roleService.findAuthIdsById(roleId);
        return Result.ok(authIdsById);
    }

    /**
     * 给角色分配菜单权限
     */
    @RequestMapping("/auth-grant")
    public Result grantAuth(@RequestBody AssignAuthDto assignAuthDto, @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token){
        //1、从token中拿到当前登陆用户的id
        int userId = tokenUtils.getCurrentUser(token).getUserId();

        //2、执行业务并返回
        roleService.insertRoleAuth(assignAuthDto, userId);
        return Result.ok("添加角色权限成功！");
    }

    /**
     * 根据角色id修改角色描述
     */
    @RequestMapping("/role-update")
    public Result updateRole(@RequestBody Role role, @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token){
        //1、从token中拿到当前登陆用户的id
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        int userId = currentUser.getUserId();

        //2、设置修改用户id
        role.setUpdateBy(userId);

        //3、执行业务并返回
        Result result = roleService.updateDescById(role);
        return result;
    }

}
